package com.qf.dao.impl;

import com.qf.dao.C_vDao;
import com.qf.entity.C_v;
import com.qf.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class C_vDaoImpl implements C_vDao {
    private QueryRunner qr = DBUtils.DB.getQr();

    @Override
    public int addC_v(C_v c_v) throws SQLException {
        String sql = "insert into c_v (c_v_vid,c_v_cid,c_v_update_time,c_v_startTime,c_v_endTime) values(?,?,?,?,?)";
        Object[] objs = {c_v.getC_v_vid(), c_v.getC_v_cid(), c_v.getC_v_update_time(), c_v.getC_v_startTime(), c_v.getC_v_endTime()};
        int add = qr.update(sql, objs);
        return add;
    }

    @Override
    public C_v selectC_vByVidAndCid(Integer vid, Integer cid) throws SQLException {
        String sql = "select * from c_v where c_v_vid=? and c_v_cid=?";
        Object[] objs = {vid, cid};
        C_v c_v = qr.query(sql, new BeanHandler<>(C_v.class), objs);
        return c_v;
    }

    @Override
    public int selectC_vCount() throws SQLException {
        String sql = "select count(1) from c_v";
        Long count = (Long) qr.query(sql, new ScalarHandler());
        return count == null ? 0 : Integer.parseInt(count + "");
    }

    @Override
    public List<C_v> selectC_vListByPage(Integer currentPageNo, Integer pageSize) throws SQLException {
        String sql = "select * from c_v limit ?,?";
        Object[] objs = {(currentPageNo - 1) * pageSize, pageSize};
        List<C_v> c_vList = qr.query(sql, new BeanListHandler<>(C_v.class), objs);
        return c_vList;

    }

    @Override
    public int deleteC_vByCid(Integer cid) throws SQLException {
        String sql = "delete from c_v where c_v_cid=?";
        int delete = qr.update(sql, cid);
        return delete;

    }
}
